package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.constants.AskforleaveConstants;
import com.zrwl.poscloud.base.constants.GradeTeacherConstants;
import com.zrwl.poscloud.base.entity.Askforleave;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.GradeTeacherR;
import com.zrwl.poscloud.base.enums.EAskforleave;
import com.zrwl.poscloud.base.enums.EMessageModel;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.enums.MessageModelType;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DateUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.EmployeeMapper;
import com.zrwl.poscloud.xo.mapper.GradeTeacherRMapper;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import com.zrwl.poscloud.xo.vo.MessageModelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.zrwl.poscloud.xo.mapper.AskforleaveMapper;
import com.zrwl.poscloud.xo.vo.AskforleaveVO;

import javax.annotation.Resource;

/**
 * 请假管理 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@Service
public class AskforleaveServiceImpl extends SuperServiceImpl<AskforleaveMapper, Askforleave> implements AskforleaveService {

    @Autowired
    private AskforleaveService askforleaveService;
    @Resource
    private AskforleaveMapper askforleaveMapper;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;

    @Autowired
    private MessageModelService messageModelService;

    @Resource
    private GradeTeacherRMapper gradeTeacherRMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public IPage<AskforleaveVO> getPageList(AskforleaveVO askforleaveVO) {
        Page<Askforleave> page = new Page<>();
        page.setCurrent(askforleaveVO.getCurrentPage());
        page.setSize(askforleaveVO.getPageSize());
        IPage<AskforleaveVO> pageList = askforleaveMapper.getPageList(page, askforleaveVO);
        List<AskforleaveVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<AskforleaveVO> getAllList(AskforleaveVO askforleaveVO) {
        List<AskforleaveVO> list = askforleaveMapper.getAllList(askforleaveVO);
        for (AskforleaveVO askForLeaveVO : list) {
            askForLeaveVO.setTypeName(AskforleaveConstants.getTypeName(askForLeaveVO.getType()));
            askForLeaveVO.setStatusName(AskforleaveConstants.getStatusName(askForLeaveVO.getStatus()));
        }
        return list;
    }

    @Override
    public String add(AskforleaveVO askforleaveVO) {
        Askforleave entity = new Askforleave();
        BeanUtils.copyProperties(askforleaveVO, entity, SysConf.STATUS);
        entity.insert();
        askforleaveVO.setUid(entity.getUid());
        //发送微信消息-请假申请通知
        MessageModelVO messageModelVO = new MessageModelVO();
        EmployeeVO employee = employeeMapper.getEmployeeByUid(askforleaveVO.getLeaveEmployeeUid());
        askforleaveVO.setLeaveEmployeeName(employee.getName());
        GradeTeacherR gradeTeacherR = gradeTeacherRMapper.getTeacherBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
        //假条申请后发给班主任
        if (gradeTeacherR != null) {
            messageModelVO.setToUid(gradeTeacherR.getEmployeeUid());
            sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
        }
        //假条申请后发给有假条权限的其他教师
        List<GradeTeacherR> gradeTeacherRList = gradeTeacherRMapper.getTeachersBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
        if (gradeTeacherRList.size() > 0) {
            for (GradeTeacherR teacherR : gradeTeacherRList) {
                if (teacherR.getLeaveCheck() == 1) {
                    messageModelVO.setToUid(teacherR.getEmployeeUid());
                    sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
                }
            }
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(AskforleaveVO askforleaveVO) {
        Askforleave entity = askforleaveService.getById(askforleaveVO.getUid());
        BeanUtils.copyProperties(askforleaveVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(AskforleaveVO askforleaveVO) {
        Askforleave entity = askforleaveService.getById(askforleaveVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<AskforleaveVO> getRecentlyAskForLeaveInfo(AskforleaveVO askforleaveVO) {
        List<AskforleaveVO> list = askforleaveMapper.getRecentlyAskForLeaveInfo(askforleaveVO);
        for (AskforleaveVO askForLeaveInfo : list) {
            askForLeaveInfo.setTypeName(AskforleaveConstants.getTypeName(askForLeaveInfo.getType()));
            askForLeaveInfo.setStatusName(AskforleaveConstants.getStatusName(askForLeaveInfo.getStatus()));
            if (askForLeaveInfo.getEndTime() == null) {
                askForLeaveInfo.setEndTime(new Date());
            }
            askForLeaveInfo.setIntervalTime(DateUtils.getDatePoor(askForLeaveInfo.getEndTime(), askForLeaveInfo.getStartTime()));
        }
        return list;
    }

    @Override
    public AskforleaveVO getAskForLeaveInfo(AskforleaveVO askforleaveVO) {
        AskforleaveVO askForLeaveInfo = askforleaveMapper.getAskForLeaveInfo(askforleaveVO);
        askForLeaveInfo.setTypeName(AskforleaveConstants.getTypeName(askForLeaveInfo.getType()));
        askForLeaveInfo.setStatusName(AskforleaveConstants.getStatusName(askForLeaveInfo.getStatus()));
        return askForLeaveInfo;
    }

    @Override
    public IPage<AskforleaveVO> getAllAskforleaveList(AskforleaveVO askforleaveVO) {
        Page<Askforleave> page = new Page<>();
        page.setCurrent(askforleaveVO.getCurrentPage());
        page.setSize(askforleaveVO.getPageSize());
        IPage<AskforleaveVO> pageList = askforleaveMapper.getAllAskforleaveList(page, askforleaveVO);
        List<AskforleaveVO> list = pageList.getRecords();
        for (AskforleaveVO askForLeaveVO : list) {
            askForLeaveVO.setLeaveDuration(DateUtils.getDatePoor(askForLeaveVO.getEndTime(), askForLeaveVO.getStartTime()));
            askForLeaveVO.setTypeName(AskforleaveConstants.getTypeName(askForLeaveVO.getType()));
            askForLeaveVO.setStatusName(AskforleaveConstants.getStatusName(askForLeaveVO.getStatus()));
        }
        pageList.setRecords(list);
        return pageList;
    }

    /**
     * 假条审核
     *
     * @param askforleaveVO 假条
     * @return 审核结果
     */
    @Override
    public String editAudit(AskforleaveVO askforleaveVO) {
        Askforleave askforleave = askforleaveService.getById(askforleaveVO.getUid());
        if (askforleave.getStatus() == EAskforleave.audited) {
            throw new RuntimeException("该假条已被审核");
        }
        GradeTeacherR gradeTeacherR = gradeTeacherRService.getTeacherAuthorityByTeacherAndStudent(askforleaveVO.getLeaveEmployeeUid(), askforleaveVO.getCheckEmployeeUid());
        if (gradeTeacherR == null || gradeTeacherR.getLeaveCheck() == 0) {
            throw new RuntimeException("该教师未授权假条审核");
        }
        askforleave.setStatus(askforleaveVO.getStatus());
        askforleave.setCheckReason(askforleaveVO.getCheckReason());
        askforleave.setCheckEmployeeUid(askforleaveVO.getCheckEmployeeUid());
        askforleave.setCheckTime(new Date());
        askforleave.updateById();
        //发送微信消息-请假审核结果通知
        MessageModelVO messageModelVO = new MessageModelVO();
        //假条审核或已驳回发给持卡人
        if (askforleaveVO.getStatus() == EAskforleave.audited || askforleaveVO.getStatus() == EAskforleave.rejected) {
            messageModelVO.setToUid(askforleaveVO.getLeaveEmployeeUid());
            sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
        }
        if (askforleaveVO.getStatus() == EAskforleave.audited) {
            gradeTeacherR = gradeTeacherRMapper.getTeacherBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
            if (gradeTeacherR != null) {
                //假条审核后发给有假条通知或假条审核权限的班主任
                if (gradeTeacherR.getLeaveInform() == 1 || gradeTeacherR.getLeaveCheck() == 1) {
                    messageModelVO.setToUid(gradeTeacherR.getEmployeeUid());
                    sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
                }
            }
            List<GradeTeacherR> gradeTeacherRList = gradeTeacherRMapper.getTeachersBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
            if (gradeTeacherRList.size() > 0) {
                for (GradeTeacherR teacherR : gradeTeacherRList) {
                    //审核后发给有假条通知或假条审核权限的其他教师
                    if (teacherR.getLeaveInform() == 1 || teacherR.getLeaveCheck() == 1) {
                        messageModelVO.setToUid(teacherR.getEmployeeUid());
                        sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
                    }
                }
            }
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 假条取消
     *
     * @param askforleaveVO 假条
     * @return 取消结果
     */
    @Override
    public String editCancel(AskforleaveVO askforleaveVO) {
        Askforleave askforleave = askforleaveService.getById(askforleaveVO.getUid());
        if (askforleave.getStatus() == EAskforleave.canceled) {
            throw new RuntimeException("该假条已取消");
        }
        askforleave.setStatus(EAskforleave.canceled);
        askforleave.setCancelReason(askforleaveVO.getCancelReason());
        askforleave.setCancelTime(new Date());
        askforleave.updateById();
        //发送微信消息-请假取消结果通知
        MessageModelVO messageModelVO = new MessageModelVO();
        //假条取消后发给持卡人
        if (askforleave.getStatus() == EAskforleave.canceled) {
            askforleaveVO.setStatus(askforleave.getStatus());
            messageModelVO.setToUid(askforleaveVO.getLeaveEmployeeUid());
            sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
        }
        //假条取消后发给有假条通知或假条审核权限的教师
        if (askforleave.getStatus() == EAskforleave.canceled) {
            //班主任
            GradeTeacherR gradeTeacherR = gradeTeacherRMapper.getTeacherBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
            if (gradeTeacherR != null) {
                if (gradeTeacherR.getLeaveInform() == 1 || gradeTeacherR.getLeaveCheck() == 1) {
                    askforleaveVO.setStatus(askforleave.getStatus());
                    messageModelVO.setToUid(gradeTeacherR.getEmployeeUid());
                    sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
                }
            }
            //其他教师
            List<GradeTeacherR> gradeTeacherRList = gradeTeacherRMapper.getTeachersBySubjectTypeAndGrade(askforleaveVO.getGradeUid(), GradeTeacherConstants.subjectType_grade_teacher);
            if (gradeTeacherRList.size() > 0) {
                for (GradeTeacherR teacherR : gradeTeacherRList) {
                    if (teacherR.getLeaveInform() == 1 || teacherR.getLeaveCheck() == 1) {
                        askforleaveVO.setStatus(askforleave.getStatus());
                        messageModelVO.setToUid(teacherR.getEmployeeUid());
                        sendAskforleaveMessages(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_LEAVE_RESULT_NOTICE, askforleaveVO);
                    }
                }
            }
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }


    /**
     * 发送假条消息
     */
    public String sendAskforleaveMessages(MessageModelVO messageModelVO, int platformType, MessageModelType messageModelType, AskforleaveVO askforleaveVO) {
        messageModelVO.setCompanyUid(askforleaveVO.getCompanyUid());
        messageModelVO.setPlatformType(platformType);
        messageModelVO.setType(messageModelType.getType());
        messageModelVO.setModelType(EMessageModel.getModelType(platformType, messageModelType));
        String statusName = AskforleaveConstants.getStatusName(askforleaveVO.getStatus());
        String typeName = AskforleaveConstants.getTypeName(askforleaveVO.getType());
        String startTime = DateUtils.formatDateAMPMS(askforleaveVO.getStartTime(), "yyyy年MM月dd日 HH:mm");
        String endTime = DateUtils.formatDateAMPMS(askforleaveVO.getEndTime(), "yyyy年MM月dd日 HH:mm");
        String date = startTime + "~" + endTime;
        String str = askforleaveVO.getLeaveEmployeeName() + "," + typeName + "," + date + "," + askforleaveVO.getContent() + "【" + statusName + "】";
        String content = MessageModelType.getParams(platformType, messageModelType, str);
        messageModelVO.setContent(content);
        return messageModelService.addSendMessage(messageModelVO);
    }
}